define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	
	
	
	
	require("cordova!plugin.http.request");
	require("cordova!com.justep.cordova.plugin.qq");
	require("cordova!com.justep.cordova.plugin.weixin.v3");
	require("cordova!com.justep.cordova.plugin.weibo");
    
    
    var weixinApp = "wx589a80cc385e9ac6";
    var weixinSecrt = "e1df9bd3ead880556ab6b316433d6e68";
    var qqAppId = "1105712052";// demo QQ app id.1105710236 me、1105114302  网易
    
	var Model = function(){
		this.callParent();
		
	};
	
	
	

	Model.prototype.loginBtnClick = function(event){
	    var me = this;
	    var userName = me.comp("username").val();
	    var password = me.comp("password").val();
	    if(userName&&password){
	       
	       // me.comp("popOver3").show(); 
	        var url = this.getParentModel().serverUrl + "/ex_community/a/api/community/login?callback=?";
			$.getJSON(
				url,
				{
					username:userName,
					password:password	
				},
				function(data,status){
				   console.log(data);
				   if(data.errNo != -1){
					   localStorage.setItem("userInfo",JSON.stringify(data.data.user));
					   if(data.data.office){
						   localStorage.setItem("officeInfo",JSON.stringify(data.data.office));
					   }
					   
					   localStorage.setItem("companyInfo",JSON.stringify(data.data.company));
					   
					   
					   if(data.data.user.userType == '4'){
						   $('.MerchantMall').show();
						   $('.bisnessInfo').show();
					   }
					   	
//				       $(".rollText").text(data.data.user.name + "欢迎您，祝心想事成，万事如意！");  
				      
				       if(me.getParentModel().comp('body').getActiveIndex() == 3){
				    	   me.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/mineFragment.w');
				    	   me.getParentModel().comp('mineContainer').refresh();
				    	  // me.getParentModel().comp('body').to(3);
				       }else{
				    	   me.getParentModel().comp("contentDialog").open({src:me.getParentModel().toPath});
				       }
				       
				       justep.Util.hint("登录成功",{delay:1000,position:"middle"});
				      
				   }else{
					  justep.Util.hint("登录失败",{delay:1000,position:"middle",type:'danger'});
				   }
				   //me.comp("popOver3").hide();								
				}					
			);
	    }else{
	    	justep.Util.hint("用户名或密码不能为空",{
	    		type:"danger"
	    	});
        }
	};
	
	Model.prototype.modelLoad = function(event){
		document.body.addEventListener("touchstart", function () { }); 

	};

	Model.prototype.backBtnClick = function(event){
		this.owner.setSrc("$UI/CommunityOto/mine/mineFragment.w");
		
		this.owner.refresh();
	};

    Model.prototype.doRegister = function(event){
    	//this.comp("rigister").show();
		justep.Shell.showPage("$UI/communityOto/mine/register.w");

	};

	Model.prototype.presonClick = function(event){
		justep.Shell.showPage("$UI/communityOto/mine/regisActivity.w");
	};
	
	Model.prototype.buniessClick = function(event){
		justep.Shell.showPage("$UI/communityOto/mine/buniessRegisActivity.w");
	};
	
	// x5 app
	Model.prototype.loginWeixinClicked = function(event) {
		var self = this;
		if (!navigator.weixin) {
			//justep.Util.hint("请安装最新版本(含插件)体验！");
			return;
		}
		
        
		var weixin = navigator.weixin;

		weixin.auth(function(info) {
		   
			self.weixinCode2Token(info);
		},
		function(reason) {
			justep.Util.hint("登录失败",{delay:1000,position:"middle",type:'danger'});
		});

	};
	

	Model.prototype.authSuccess = function(access_token, openid) {
		// https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
		
		var me = this;
		if (!(access_token && openid)) {
			justep.Util.hint("登录失败",{delay:1000,position:"middle",type:'danger'});
			return;
		}

		var url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid +"lang=zh-CN";
		
		
		
		
		var httpReq = new plugin.HttpRequest();
		httpReq.getJSON(url, function(status, data) {
		    
			if (status) {
				justep.Util.hint("登录失败:", {
					"type" : "danger"
				});
			}
			
			var openId = data.openid;
			//alert('openid',openId);
			var url = 'http://www.xjzlhy.com:81/ex_community/a/api/community/selectOpenId?callback=?';
			var user = {};
			user.openId = data.openid;
			
			user.accountType = "WX";
			user.name = data.nickname || "NONAME";
           
			$.getJSON(url,{openId:openId},function(data){
			   
			    if(data.errNo == 2){
			    	me.getParentModel().user  = user;
			    	me.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/bindUser.w');
			    	me.getParentModel().comp('mineContainer').refresh();
			    	justep.Util.hint(data.errMessage,{type:'danger',delay:1000,position:"middle"});
			    }else{
			    	localStorage.setItem("userInfo",JSON.stringify(data.data.user));
					   localStorage.setItem("officeInfo",JSON.stringify(data.data.office));
					   localStorage.setItem("companyInfo",JSON.stringify(data.data.company));
				    if(data.data.user.userType == '4'){
					   $('.MerchantMall').show();
					   $('.bisnessInfo').show();
				    }	
			        $(".rollText").text(data.data.user.name + "欢迎您，祝心想事成，万事如意！");  
			        if(me.getParentModel().comp('body').getActiveIndex() == 3){
			         
			    	   me.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/mineFragment.w');
			    	   me.getParentModel().comp('mineContainer').refresh();
			    	   //me.getParentModel().comp('body').to(3);
			        }else{
			          
			    	   me.getParentModel().comp("contentDialog").open({src:me.getParentModel().toPath});
			        }
			        justep.Util.hint("登录成功",{delay:1000,position:"middle"});
			       
			       
			    }			
			});
			
			// 微信的返回内容
			// "openid":"OPENID",
			// "nickname":"NICKNAME",
			// "sex":1,
			// "province":"PROVINCE",
			// "city":"CITY",
			// "country":"COUNTRY",
			// "headimgurl":
			// "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
			// "privilege":[
			// "PRIVILEGE1",
			// "PRIVILEGE2"
			// ],
			// "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"

		});
	};

	Model.prototype.weixinCode2Token = function(info) {
		var code = info.code;
		var url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + weixinApp + "&secret=" + weixinSecrt + "&code=" + code + "&grant_type=authorization_code";
        var me = this;
		var httpReq = new plugin.HttpRequest();
		httpReq.getJSON(url, function(status, data) {
           
			if (status) {
				justep.Util.hint("登录失败",{delay:1000,position:"middle",type:'danger'});
				return;
			}
			
			//alert('data',JSON.stringify(data));
			var unionid = data.unionid || "none";
			var openid = data.openid || "none";
			//alert('openId',openid);
			var access_token = data.access_token;
			me.authSuccess(access_token, openid);
		});

	};
	
	

	Model.prototype.loginQQClick = function(event) {
		if (!navigator.QQ) {
			justep.Util.hint("请安装最新版本(含插件)体验！");
			return;
		}
		
		var me = this;
		function success(info) {
			if (info.userid && info.access_token) {
            var qqId = info.userid;
			var url = 'http://www.xjzlhy.com:81/ex_community/a/api/community/selectOpenId?callback=?';
			var user = {};
			user.openId = info.userid;
			user.accountType = "QQ";
			user.name = info.nickname || "NONAME";
          
			$.getJSON(url,{qqId:qqId},function(data){
			    if(data.errNo == 2){
			    	me.getParentModel().user  = user;
			    	me.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/bindUser.w');
			    	me.getParentModel().comp('mineContainer').refresh();
			    	justep.Util.hint(data.errMessage,{type:'danger',delay:1000,position:"middle"});
			    }else{
			    	   localStorage.setItem("userInfo",JSON.stringify(data.data.user));
					   localStorage.setItem("officeInfo",JSON.stringify(data.data.office));
					   localStorage.setItem("companyInfo",JSON.stringify(data.data.company));
				    if(data.data.user.userType == '4'){
					   $('.MerchantMall').show();
					   $('.bisnessInfo').show();
				    }	
			        $(".rollText").text(data.data.user.name + "欢迎您，祝心想事成，万事如意！");  
			        if(me.getParentModel().comp('body').getActiveIndex() == 3){
			         
			    	   me.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/mineFragment.w');
			    	   me.getParentModel().comp('mineContainer').refresh();
			    	   //me.getParentModel().comp('body').to(3);
			        }else{
			          
			    	   me.getParentModel().comp("contentDialog").open({src:me.getParentModel().toPath});
			        }
			        justep.Util.hint("登录成功",{delay:1000,position:"middle"});
			       
			       
			    }			
			});

			}
		}
		navigator.QQ.ssoLogin(success, function(info) {
			justep.Util.hint("登录失败",{delay:1000,position:"middle",type:'danger'});
		});

	};
	
	Model.prototype.oathQQInfo = function(appid, token, openid, success, error) {
		var qqUrl = "https://graph.qq.com/user/get_user_info?" + "access_token=" + token + "&" + "oauth_consumer_key=" + appid + "&" + "openid=" + openid + "&format=json";
		var httpReq = new plugin.HttpRequest();
		httpReq.getJSON(qqUrl, function(status, data) {
			// 这里返回用户的信息
			
		    
			if (status) {
				justep.Util.hint("登录失败:", {
					"type" : "danger"
				});
			}
			var qqId = data.openid;
			var url = 'http://www.xjzlhy.com:81/ex_community/a/api/community/selectOpenId?callback=?';
			var user = {};
			user.qqId = data.openid;
			user.accountType = "QQ";
			user.name = data.nickname || "NONAME";
          
			$.getJSON(url,{qqId:openid},function(data){
			    if(data.errNo == 2){
			    	me.getParentModel().qqUser  = user;
			    	me.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/bindUser.w');
			    	me.getParentModel().comp('mineContainer').refresh();
			    	justep.Util.hint(data.errMessage,{type:'danger',delay:1000,position:"middle"});
			    }else{
			    	localStorage.setItem("userInfo",JSON.stringify(data.data.user));
					   localStorage.setItem("officeInfo",JSON.stringify(data.data.office));
					   localStorage.setItem("companyInfo",JSON.stringify(data.data.company));
				    if(data.data.user.userType == '4'){
					   $('.MerchantMall').show();
					   $('.bisnessInfo').show();
				    }	
			        $(".rollText").text(data.data.user.name + "欢迎您，祝心想事成，万事如意！");  
			        if(me.getParentModel().comp('body').getActiveIndex() == 3){
			         
			    	   me.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/mineFragment.w');
			    	   me.getParentModel().comp('mineContainer').refresh();
			    	   //me.getParentModel().comp('body').to(3);
			        }else{
			          
			    	   me.getParentModel().comp("contentDialog").open({src:me.getParentModel().toPath});
			        }
			        justep.Util.hint("登录成功",{delay:1000,position:"middle"});
			       
			       
			    }			
			});
		
			success(data.nickname);
		});
	};
	Model.prototype.backButtonClick = function(event){
	    if('current' in (this.getParent()) && this.getParentModel().current !== 3){
	    	this.owner.setSrc(this.getParentModel().currentPath);
	    	this.owner.refresh();
	    }else{
	    	justep.Shell.closePage();
	    
	    }
		
	};
	
	
	//微博登录
	Model.prototype.loginWeiboClick = function(event) {
		if (!navigator.Weibo) {
			justep.Util.hint("请安装最新版本(含插件)体验！");
			return;
		}
		

		var me = this;
		var onSuccess = function(info) {
			if (!info) {
				justep.Util.hint("登录失败 ", {
					"type" : "danger"
				});
				return;
			}
			var user = {};
			user.userid = info.id;
			user.accountType = "Weibo";
			user.name = info.name || "NONAME";
			
			var weiboId = info.id;
			var url = 'http://www.xjzlhy.com:81/ex_community/a/api/community/selectOpenId?callback=?';
            $.getJSON(url,{sinaId:weiboId},function(data){
			    if(data.errNo == 2){
			    	me.getParentModel().user  = user;
			    	me.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/bindUser.w');
			    	me.getParentModel().comp('mineContainer').refresh();
			    	justep.Util.hint(data.errMessage,{type:'danger',delay:1000,position:"middle"});
			    }else{
			    	localStorage.setItem("userInfo",JSON.stringify(data.data.user));
					   localStorage.setItem("officeInfo",JSON.stringify(data.data.office));
					   localStorage.setItem("companyInfo",JSON.stringify(data.data.company));
				    if(data.data.user.userType == '4'){
					   $('.MerchantMall').show();
					   $('.bisnessInfo').show();
				    }	
			        $(".rollText").text(data.data.user.name + "欢迎您，祝心想事成，万事如意！");  
			        if(me.getParentModel().comp('body').getActiveIndex() == 3){
			         
			    	   me.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/mineFragment.w');
			    	   me.getParentModel().comp('mineContainer').refresh();
			    	   //me.getParentModel().comp('body').to(3);
			        }else{
			          
			    	   me.getParentModel().comp("contentDialog").open({src:me.getParentModel().toPath});
			        }
			        justep.Util.hint("登录成功",{delay:1000,position:"middle"});
			       
			       
			    }			
			});
			

		};

		navigator.Weibo.ssoLogin(onSuccess, function(info) {
			justep.Util.hint("登录失败",{delay:1000,position:"middle",type:'danger'});
		});

	};
	
	
	
	Model.prototype.smsRegisterClick = function(event){
	
		this.getParentModel().comp('mineContainer').setSrc('$UI/communityOto/mine/smsRegister.w');
	    this.getParentModel().comp('mineContainer').refresh();

	};
	
	
	
	Model.prototype.doReset = function(event){
	
		justep.Shell.showPage("$UI/communityOto/mine/resetPassword.w");

	};
	
	
	
	return Model;
});